package com.zkyc.framework.user.controller;


import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.zkyc.framework.common.dto.PageDto;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.User;
import com.zkyc.framework.common.utils.SCaptcha;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.user.*;
import com.zkyc.framework.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * (User)表控制层
 *
 * @author
 * @since 2022-05-17 15:44:55
 */
@RestController
@RequestMapping
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userVo
     * @return
     */
    @PostMapping("login")
    @Log(type = LogType.登入, detail = "用户登入")
    public ResultVO login(@RequestBody UserVo userVo,HttpSession session) {
        return userService.login(userVo,session);
    }

    /**
     * 修改密码
     *
     * @param changePasswordVo
     * @return
     */
    @PostMapping("changePassword")
    @Log(type = LogType.修改密码, detail = "修改密码")
    public ResultVO changePassword(@RequestBody ChangePasswordVo changePasswordVo) {
        return userService.changePassword(changePasswordVo);
    }

    /**
     * 登出
     *
     * @param servletRequest
     * @return
     */
    @GetMapping("logout")
    @Log(type = LogType.登出, detail = "用户登出")
    public ResultVO logout(ServletRequest servletRequest) {
        return userService.logout(servletRequest);
    }

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("queryById")
    public ResultVO queryById(@RequestParam String id) {
        User user = userService.queryById(id);
        if (user == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        return ResultVO.data(user);
    }

    /**
     * 分页查询角色列表
     *
     * @return
     */
    @PostMapping("list")
    public ResultVO getUserList(@RequestBody UserPageVo userPageVo) {
        return userService.getUserList(userPageVo);
    }

    /**
     * @param
     * @return 新增用户
     */
    @PostMapping("save")
    @Log(type = LogType.新建用户)
    public ResultVO add(@RequestBody UserRoleVo userRoleVo) {
        return userService.insert(userRoleVo);
    }

    /**
     * 编辑用户
     *
     * @param
     */
    @PostMapping("edit")
    @Log(type = LogType.编辑用户)
    public ResultVO edit(@RequestBody UserRoleVo userRoleVo) {
        return userService.update(userRoleVo);
    }


    /**
     * 删除用户
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("delete")
    public ResultVO deleteById(@RequestParam String id) {
        return userService.deleteById(id);
    }


    /**
     * 启用与禁用
     *
     * @param userId
     * @param type
     * @return
     */
    @GetMapping("forbidden")
    @Log(type = LogType.启用禁用用户)
    public ResultVO forbidden(@RequestParam String userId, @RequestParam Integer type) {
        return userService.forbidden(userId, type);
    }


    /**
     * 根据用户id找到该网站下的角色列表
     *
     * @param userId
     * @return
     */
    @GetMapping("getRoleByUserId")
    public ResultVO getRoleByUserId(@RequestParam String userId) {
        return userService.getRoleByUserId(userId);
    }


    /**
     * 根据网站id查询权限列表
     *
     * @param websiteId
     * @return
     */
    @GetMapping("getRightByWebsite")
    public ResultVO getRightByWebsite(@RequestParam Integer websiteId) {
        return userService.getRightByWebsite(websiteId);
    }

    /**
     * 获取当前用户该网站下的权限
     *
     * @param websiteId
     * @return
     */
    @GetMapping("getUserRightByWebsiteId")
    public ResultVO getUserRightByWebsite(@RequestParam Integer websiteId) {
        return userService.getUserRightByWebsite(websiteId);
    }

    /**
     * 查看按钮 根据用户id查询权限列表
     *
     * @param userId
     * @return
     */
    @GetMapping("getRoleListByUserId")
    public ResultVO getRoleListByUserId(@RequestParam String userId) {
        return userService.getRoleListByUserId(userId);
    }



    /**
     * 根据用户id查询权限列表
     *
     * @param userId
     * @return
     */
    @GetMapping("queryRightByUserId")
    public ResultVO queryRightByUserId(@RequestParam String userId) {
        return userService.queryRightByUserId(userId);
    }


    /**
     * 重置密码
     *
     * @param userId
     * @return
     */
    @Log(type = LogType.重置密码)
    @GetMapping("resetPassword")
    public ResultVO resetPassword(@RequestParam String userId) {
        return userService.resetPassword(userId);
    }

    /**
     * 判断用户是否为管理员用户
     *
     * @param userIdVo
     * @return
     */
    @PostMapping("judgeAdminUser")
    public ResultVO judgeAdminUser(@RequestBody UserIdVo userIdVo) {
        return userService.judgeAdminUser(userIdVo);
    }


    /**
     * 判断用户是否为管理员用户
     *
     * @param websiteId
     * @return
     */
    @GetMapping("judgeRightExist")
    public ResultVO judgeRightExist(@RequestParam Integer  websiteId,@RequestParam String userId) {
        return userService.judgeRightExist(websiteId,userId);
    }


    /**
     * @date
     * @description 生成图片验证码
     */
    @RequestMapping(value = "/queryVerification", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void verification(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //实例生成验证码对象
        SCaptcha instance = new SCaptcha();
        //将验证码存入session
        session.setAttribute("verification", instance.getCode());
        //向页面输出验证码图片
        instance.write(response.getOutputStream());
    }

    /**
     * 获取用户在该网站下的按钮权限
     * @param websiteId
     * @param userId
     * @return
     */
    @GetMapping("getUserWebMenu")
    public ResultVO getUserWebMenu(@RequestParam Integer  websiteId,@RequestParam String userId) {
        return userService.getUserWebMenu(websiteId,userId);
    }

    @GetMapping("getWebInfoByUserId")
    public ResultVO getWebInfoByUserId(@RequestParam String userId) {
        return userService.getWebInfoByUserId(userId);
    }
    @GetMapping("getCenterInfoByCenterId")
    public ResultVO getCenterInfoByCenterId(@RequestParam Integer centerId) {
        return userService.getCenterInfoByCenterId(centerId);
    }
    @GetMapping("judgeUserIsAdmin")
    public ResultVO judgeUserIsAdmin(@RequestParam String  userId) {
        return userService.judgeUserIsAdmin(userId);
    }

}

